package com.xiaocikuai.smallchunks.mapper.Transaction;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xiaocikuai.smallchunks.entity.Product.Product;
import com.xiaocikuai.smallchunks.entity.Transaction.*;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

@Mapper
public interface TransactionMapper  extends BaseMapper<TransactionMapper> {

    //获取用户地址列表
    @Select("SELECT * FROM address WHERE uid = #{uid} ")
    List<Address> getAddress(int uid);

    @Select("SELECT * FROM address WHERE aid = #{aid} ")
    Address getAddressInfo(int aid);
    //添加地址
    @Insert("insert into address (uid, consignee, telephone, delivery, prov, city, area) values (#{uid}, #{consignee}, #{telephone}, #{delivery}, #{prov}, #{city}, #{area})")
    boolean addAddress(Address address);

    //修改地址
    @Update("update address set consignee = #{consignee}, telephone = #{telephone}, delivery = #{delivery} ,prov = #{prov}, city = #{city}, area = #{area} where aid = #{aid} and uid = #{uid} ")
    boolean updateAddress(Address address);

    //删除地址
    @Delete("delete from address where aid = #{aid} and uid = #{uid}")
    boolean deleteAddress(Address address);



    //生成订单
    @Insert("insert into yshop_order ( order_id, uid, tid, otype, create_device, status, create_time, update_time) values ( #{orderId}, #{uid}, #{tid}, #{otype}, #{createDevice}, #{status}, #{createTime}, #{updateTime})")
    @SelectKey(statement = "SELECT LAST_INSERT_ID() ", keyProperty = "oid", resultType = Integer.class, before = false)
    int addOrder(YshopOP yshopOP);

    //新增订单
    @Insert("insert into yshop_pay ( uid, oid, bid, aid, total_price, promotional_id, promotional_price, pay_price, paid, pay_time, pay_type, create_time, update_time, refund_status, refund_price, mark) values ( #{uid}, #{oid}, #{bid}, #{aid}, #{totalPrice}, #{promotionalId}, #{promotionalPrice}, #{payPrice}, #{paid}, #{payTime}, #{payType}, #{createTime}, #{updateTime}, #{refundStatus}, #{refundPrice}, #{mark})")
    @SelectKey(statement = "SELECT LAST_INSERT_ID() ", keyProperty = "payId", resultType = Integer.class, before = false)
    boolean addYShopPay(YshopOP yshopOP);


    //是否有优惠
    @Select("SELECT * FROM yshop_promotional WHERE promotional_id = #{promotionalId}")
    boolean checkPromotionalId(int promotionalId);

    //获取优惠券过期时间
    @Select("SELECT expire_time FROM yshop_promotional WHERE promotional_id =  #{promotionalId} ")
    Date getExpireTime(int promotionalId);

    //获取适用产品列表
    @Select("SELECT tid_list FROM yshop_promotional WHERE promotional_id =  #{promotionalId} ")
    String getTidList(int promotionalId);

    //验证用户是否拥有该优惠券
    @Select("SELECT count(*) FROM yshop_promotional_user WHERE promotional_id = #{promotionalId} AND uid = #{uid}")
    boolean couponValidation(int promotionalId, int uid);


    //判断支付订单已生成
    @Select("SELECT * FROM yshop_pay WHERE uid = #{uid} AND oid = #{oid}")
    Integer isYShopPay(int uid, int oid);

    //获取商品是否优惠
    @Select("Select pre_time FROM yshop_male_list WHERE tid = #{tid}")
    Date getPreTime(int tid);

    //获取商品优惠价
    @Select("Select pre_price FROM yshop_male_list WHERE tid = #{tid}")
   Integer getPrePrice(int tid);

    //获取商品日常价
    @Select("Select sale_price FROM yshop_male_list WHERE tid = #{tid}")
    Integer getSalePrice(int tid);


    @Update("UPDATE yshop_order SET pay_id  = #{payId} WHERE oid = #{oid} AND uid = #{uid} ")
    boolean updatePayId(YshopOP yshopOP);

    //获取订单列表
    @Select("SELECT o.*, p.total_price as payTotalPrice, p.pay_type, p.pay_time , p.pay_price, u.nickname as uNickname , u.username as uUsername , a.consignee as aRealname, a.telephone as aPhone, a.delivery as address, t.name as tName " +
            "FROM yshop_order o " +
            "WHERE uid = #{uid} " +
            "LEFT OUTER JOIN yshop_pay p ON o.oid = p.oid " +
            "LEFT OUTER JOIN user u ON o.uid = u.uid " +
            "LEFT OUTER JOIN address a ON p.aid = a.aid " +
            "LEFT OUTER JOIN yshop_male_list t ON o.tid = t.tid ")
    List<ReturnOrderList> orderListReturn(int uid);

  /*  @Select("SELECT o.*, p.total_price as payTotalPrice, p.pay_type, p.pay_time, p.pay_price, u.nickname as uNickname, u.username as uUsername, a.consignee as aRealname, a.telephone as aPhone, a.delivery as address, t.name as tName " +
            "FROM yshop_order o " +
            "LEFT OUTER JOIN yshop_pay p ON o.oid = p.oid " +
            "LEFT OUTER JOIN user u ON o.uid = u.uid " +
            "LEFT OUTER JOIN address a ON p.aid = a.aid " +
            "LEFT OUTER JOIN yshop_male_list t ON o.tid = t.tid " +
            "WHERE o.uid = #{uid}")
    List<ReturnOrderList>orderListReturn(int uid);*/
    //获取优惠金额
    @Select("SELECT promotional_price FROM yshop_promotional WHERE promotional_id = #{promotionalId}")
    Integer getPromotionalPrice(int promotionalId);

    //获取总价格
    @Select("SELECT total_price FROM yshop_pay WHERE oid = #{oid} ")
    int getTotalPrice(int oid);

//    //更新支付订单
//    @Update("update yshop_pay set count = #{count}, aid = #{aid}, promotional_id = #{promotionalId}, promotional_price = #{promotionalPrice}, pay_price = #{payPrice}, paid = #{paid}, pay_time = #{payTime}, pay_type = #{payType}, update_time = #{updateTime}, refund_status = #{refundStatus}, refund_price = #{refundPrice}, mark = #{mark} WHERE oid = #{oid} AND uid = #{uid} ") // 这里添加合适的条件
//    boolean updateYShopPay(@RequestBody YshopPay yshopPay);
//
//    //更新支付订单
//    @Update("update yshop_pay set  count = #{count}, aid = #{aid}, promotional_price = #{promotionalPrice} WHERE oid = #{oid} AND uid = #{uid} ")
//    boolean updatePayPrice(@RequestBody YshopPay yshopPay);

    @Update("UPDATE yshop_pay set paid = #{paid}, bid = #{bid}, pay_time = #{payTime}, pay_type = #{payType}, update_time = #{updateTime}, pay_price = #{payPrice}   WHERE oid = #{oid} AND uid = #{uid} ")
    void updateYShopPayss(YshopOP yshopOP);

    @Update("UPDATE yshop_order set status = #{status}, bid = #{bid}, update_time = #{updateTime}  WHERE oid = #{oid} AND uid = #{uid} ")
    void updateYShopOrderss(YshopOP yshopOP);


    //获取订单
    @Select("SELECT * FROM yshop_order WHERE oid = #{oid} and uid = #{uid} ")
    YshopOrder checkOrder(int uid, int oid);

    //获取支付订单
    @Select("SELECT * FROM yshop_pay WHERE oid = #{oid} and uid = #{uid} ")
    YshopPay checkPay(int uid, int oid);

    //订单联表
    @Select("SELECT *\n" +
            "FROM yshop_order\n" +
            "JOIN yshop_pay ON yshop_order.oid = yshop_pay.oid AND yshop_order.pay_id = yshop_pay.pay_id " +
            "JOIN yshop_male_list ON yshop_male_list.tid = yshop_order.tid " +
            "WHERE yshop_pay.oid = #{oid} ")
    YshopOP getOrderInfo(int oid, int payId);

    @Select("SELECT *\n" +
            "FROM yshop_order\n" +
            "JOIN yshop_pay ON yshop_order.oid = yshop_pay.oid AND yshop_order.pay_id = yshop_pay.pay_id " +
            "JOIN yshop_male_list ON yshop_male_list.tid = yshop_order.tid " +
            "WHERE yshop_order.order_id = #{orderId} ")
    YshopOP getOrderInfos(String orderId);


    //获取优惠券
    @Select("SELECT promotional_id FROM yshop_promotional_user WHERE uid = #{uid} AND ifdis = 0")
    List<Integer> getYshopPromotional(int uid);

    @Select("SELECT * FROM yshop_promotional WHERE promotional_id = #{promotionId} ")
    YshopPromotional getYshopPromotionalList(int promotionId);



    //获取产品
    @Select("SELECT * FROM product WHERE pid = #{pid}")
    Product getProduct(int pid);


    //获取优惠券适用的商品列表
    @Select("SELECT tid FROM yshop_redemption_code WHERE code = #{code}")
    String getCodeTidS(String code);

    @Select("SELECT * FROM yshop_order WHERE oid = #{oid}")
    YshopOrder getYShopOrder(int oid);

    @Insert("insert into yshop_bill (uid, pay_id, oid, pm, category, type, number, status, mark, create_time, update_time) " +
            "values (#{uid}, #{payId}, #{oid}, #{pm}, #{category}, #{type}, #{number}, #{status}, #{mark}, #{createTime}, #{updateTime})")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "bid", resultType = Integer.class, before = false)
    void insertYShopBillAndGetBid(YshopBill yshopBill);


    @Insert("INSERT INTO yshop_virtual_order (uid, pid, use_type, status, create_time, ifdis) VALUES (#{uid}, #{pid}, #{useType}, #{status}, #{createTime}, #{ifdis})")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "voId", resultType = Integer.class, before = false)
    void addVirtualOrder(YshopVirtualOrder yshopVirtualOrder);

    @Insert("INSERT INTO yshop_real_order (uid, pid, create_time, update_time, status, aid, mark) VALUES (#{uid}, #{pid}, #{createTime}, #{updateTime}, #{status}, #{aid}, #{mark})")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "roId", resultType = Integer.class, before = false)
    void  addRealOrder(YshopRealOrder yshopRealOrder );


    @Select("SELECT * " +
            "FROM yshop_bill b " +
            "JOIN yshop_pay p ON b.oid = p.oid AND b.oid = #{oid} AND b.status = 1 AND p.oid = #{oid} AND p.paid = 1 " +
            "JOIN yshop_order o ON b.oid = o.oid AND o.oid = #{oid} AND o.status = 1")
    boolean checkIsPay(int oid);

    @Delete("DELETE FROM yshop_redemption_code WHERE code = #{code}")
    void deleteRedemptionCode(String code);


    @Select("SELECT * FROM yshop_order " +
            "WHERE tid = #{tid} AND uid = #{uid} AND create_time >= DATE_SUB(now(), INTERVAL 24 HOUR) AND status = 0 ")
    YshopOrder isAddOrder(int tid, int uid);

    //获取订单列表
    @Select("SELECT o.*, p.total_price as payTotalPrice, p.pay_type, p.pay_time, p.pay_price, u.nickname as uNickname, u.username as uUsername, a.consignee as aRealname, a.telephone as aPhone, a.delivery as address, t.name as tName " +
            "FROM yshop_order o " +
            "LEFT OUTER JOIN yshop_pay p ON o.oid = p.oid " +
            "LEFT OUTER JOIN user u ON o.uid = u.uid " +
            "LEFT OUTER JOIN address a ON p.aid = a.aid " +
            "LEFT OUTER JOIN yshop_male_list t ON o.tid = t.tid " +
            "WHERE o.uid = #{uid}")
    List<ReturnOrderList> orderUserListReturn(int uid);

    @Select("SELECT pid FROM yshop_virtual_order WHERE uid = #{uid}")
    List<Integer> getUserE(int uid);

}
